//
//  StatusRetweetedView.swift
//  SinaWeibo
//
//  Created by mac on 15/12/27.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit
class StatusRetweetedView: UIView {


    
    // MARK:定义外部模型属性
    //记录底部视图越是
    var bottomConstraints: Constraint?
    var retweedStatus :Status? {
        didSet {
            
            //retweetedLabel.text = retweedStatus?.text
             retweetedLabel.text = "@" + (retweedStatus?.user?.name ?? "") + (": ") + (retweedStatus?.text ?? "")
            pictureView.imageURLs = retweedStatus?.imageURLs
          
           
            
  //重新添加约束之前要卸载掉以前的约束 再去重新添加
            self.bottomConstraints?.uninstall()
            
            //判断转发微博是否有配图
            if let urls = retweedStatus?.imageURLs where urls.count > 0 {
                //有配图视图 重新添加约束
             self.snp_makeConstraints { (make) -> Void in
                //1.添加约束
                //2.记录底部约束
                self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatuCellMarigin).constraint
  // print("retweed\(retweedStatus?.imageURLs)")
                }
            }else {
                //没有配图视图 重新添加约束 
                self.snp_makeConstraints { (make) -> Void in

                self.bottomConstraints = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatuCellMarigin).constraint
                }
            }
        }
    }
    
    
    // MARK:重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        backgroundColor = UIColor(white: 0.95, alpha: 1)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:设置页面 和 布局
    private func setupUI() {
      addSubview(retweetedLabel)
        addSubview(pictureView)
        //添加约束 
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(StatuCellMarigin)
            
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatuCellMarigin)
            make.left.equalTo(retweetedLabel.snp_left)
        }
        //转发微博自动计算行高 
        self.snp_makeConstraints { (make) -> Void in
           // make.bottom.equalTo(pictureView.snp_bottom).offset(StatuCellMarigin)
            self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatuCellMarigin).constraint
        }
    }



// MARK: 懒加载所有的子视图 
    private lazy var retweetedLabel:UILabel = UILabel(title: "转发微博", color:  UIColor.darkGrayColor(), fontSize: 14, margin: StatuCellMarigin)
    //配图视图
    private lazy var pictureView:StatusPictureView = StatusPictureView()
}
